package util.android.widgets;

import com.androidutil.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LayoutSeccion 
extends LinearLayout
{
	//private final int margen; 
	private final LinearLayout layout; 
	private final TextView tvTitulo;

	public LayoutSeccion(Context context, AttributeSet attrs) {
		super(context,attrs);
		
		//Resources r = getResources();				
		this.setOrientation(LinearLayout.VERTICAL);
		
		/*
		margen = (int) r.getDimension(R.dimen.margen_layout_seccion);
		this.setBackgroundColor(r.getColor(R.color.azul_oscuro));
		this.setPadding(margen, 0, margen, margen);//Si funciona
		*/
		
		layout = (LinearLayout) View.inflate(getContext(), R.layout.layout_seccion, null);
		tvTitulo = (TextView) layout.findViewWithTag("titulo");
		layout.setVisibility(View.GONE);
		
		//Agregando el titulo
		LayoutParams p = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
		this.addView(layout,p);
		
		TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.AbstractView, 0, 0);
	    try {
	        String s = ta.getString(R.styleable.AbstractView_etiqueta);
	        if(s!=null)
	        	setTitulo(s);
	    } finally {
	        ta.recycle();
	    }
	}
	
	@Override
	public void setLayoutParams(android.view.ViewGroup.LayoutParams params) {
		//AndroidUtil.mostrarToast(getContext(), "setLayoutParams");
		
		//ViewPager.LayoutParams vp;
		if(params instanceof LayoutParams) {
			LayoutParams p = (LayoutParams)params;
			
			//GRAVEDAD DEFECTO
			if(p.gravity<0) {
				p.gravity = Gravity.CENTER_HORIZONTAL;
			}
		}
		super.setLayoutParams(params);
	}
	
	/**
	 * Se ejecuta al agregar cada hijo
	 */
	/*
	@Override
	public void addView(View child, android.view.ViewGroup.LayoutParams params) {
		//AndroidUtil.mostrarToast(getContext(),"addView");
		
		//Agregando cada componente con su margen
		LayoutParams p = (LayoutParams)params;	
		//p.setMargins(margen,margen,margen,margen);
		p.topMargin = margen;
		//p.bottomMargin = margen;		
		super.addView(child, params);		
	}*/
	
	/*
	@Override
	protected boolean addViewInLayout(View child, int index, android.view.ViewGroup.LayoutParams params) {
		AndroidUtil.mostrarToast(getContext(),"addViewInLayout");
		return super.addViewInLayout(child, index, params);
	}

	

	@Override
	public void addView(View child, int index, android.view.ViewGroup.LayoutParams params) {
		AndroidUtil.mostrarToast(getContext(),"addView3");
		super.addView(child, index, params);
	}

	@Override
	public void addView(View child) {
		AndroidUtil.mostrarToast(getContext(),"addView1");
		super.addView(child);
	}

	@Override
	protected void attachViewToParent(View child, int index,
			android.view.ViewGroup.LayoutParams params) {
		AndroidUtil.mostrarToast(getContext(),"attachViewToParent");
		super.attachViewToParent(child, index, params);
	}
		*/
		
	
	public LayoutSeccion setTituloByStringId(int stringID) {
		return setTitulo(this.getResources().getString(stringID));
	}
	
	public LayoutSeccion setTitulo(String nombre) {
		tvTitulo.setText(nombre);
		layout.setVisibility(View.VISIBLE);
		return this;
	}

}
